{% extends "wuliu/_layout.html" %}
{% load wuliu_extras %}
{% block title %}修改密码{% endblock %}
{% block header_title %}修改密码{% endblock %}
          {% block content %}
            <form action="{% url 'wuliu:change_password' %}" id="form-change_password" class="form col-12 mb-2" method="post">
                <fieldset>
                    {% csrf_token %}
                    <div class="row">
                        {% show_form_input_field form.old_password "" "col-12 col-md-7" %}
                        <div class="col-12 col-md-5 align-self-end text-md">
                            <span id="old_password_note"></span>
                        </div>
                        {% show_form_input_field form.new_password "" "col-12 col-md-7" %}
                        {% show_form_input_field form.new_password_again "" "col-12 col-md-7" %}
                        <div class="col-12 col-md-5 align-self-end text-md">
                            <span id="new_password_again_note"></span>
                        </div>
                        <div class="col-12 mt-2">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search"> 确定</i>
                            </button>
                        </div>
                    </div>
                </fieldset>
            </form>
            <script>
$(document).ready(function() {
  let old_password_flag = false;
  let new_password_flag = false;
  $('#form-change_password [name="old_password"]').change(function() {
    $.post(
      "{% url 'wuliu:api_check_old_password' %}",
      {"old_password": $(this).val()},
      function(data, status) {
        if (data.code === 200) {
          if (data.data.message === "success") {
            $("#old_password_note").html('<i class="ri-checkbox-circle-fill"></i>').attr("class", "text-success");
            old_password_flag = true;
          } else {
            $("#old_password_note").html('<i class="ri-close-circle-fill"></i> 旧密码错误！').attr("class", "text-danger");
            old_password_flag = false;
          }
        } else {
          $("#old_password_note").html('');
          old_password_flag = false;
          mdtoast_error(data.data.message);
        }
      }
    );
  });
  $('#form-change_password [name="new_password_again"]').change(function() {
    let new_password = $('#form-change_password [name="new_password"]').val().trim();
    let new_password_again = $(this).val().trim();
    if (new_password && new_password_again) {
      if (new_password === new_password_again) {
        $("#new_password_again_note").html('<i class="ri-checkbox-circle-fill"></i>').attr("class", "text-success");
        new_password_flag = true;
      } else {
        $("#new_password_again_note").html('<i class="ri-close-circle-fill"></i> 两次输入的新密码不一致！').attr("class", "text-danger");
        new_password_flag = false;
      }
    }
  });
  $("#form-change_password").submit(function(e) {
    if (old_password_flag && new_password_flag) return;
    e.preventDefault();
  });
});
            </script>
          {% endblock %}
